#include <stdio.h>
#include "gpio_if.h"
#include "hdf_log.h"
#include "osal_irq.h"
#include "osal_time.h"
#include "AdcFunction.h"


#ifdef HDF_LOG_ON
#define LOGD(format, ...)                                                    \
    do {                                                                             \
            HDF_LOGD("[%{public}s:%{public}d] " format "\n", \
                __FUNCTION__, __LINE__,                               \
                ##__VA_ARGS__);                                                      \
    } while (0)

#define LOGI(format, ...)                                                                    \
    do {                                                                                     \
            HDF_LOGI("[%{public}s:%{public}d] " format "\n", __FUNCTION__, __LINE__, \
                ##__VA_ARGS__);                                                                  \
    } while (0)

#define LOGW(format, ...)                                                                    \
    do {                                                                                     \
        HDF_LOGW("[%{public}s:%{public}d] " format "\n", __FUNCTION__, __LINE__, \
            ##__VA_ARGS__);                                                                  \
    } while (0)

#define LOGE(format, ...)                                   \
    do {                                                            \
        HDF_LOGE(                                       \
            "\033[0;32;31m"                                         \
            "[%{public}s:%{public}d] " format "\033[m"   \
            "\n",                                                   \
            __FUNCTION__,  __LINE__, ##__VA_ARGS__);  \
    } while (0)
#else
#define LOGD(format, ...)                                   \
    do {                                                    \
            printf("[%s:%d] " format "\n",  \
                __FUNCTION__, __LINE__,                     \
                ##__VA_ARGS__);                             \
    } while (0)

#define LOGI(format, ...)                                                      \
    do {                                                                       \
        printf("[%s:%d] " format "\n", __FUNCTION__, __LINE__, \
            ##__VA_ARGS__);                                                    \
    } while (0)

#define LOGW(format, ...)                                                      \
    do {                                                                       \
        printf("[%s:%d] " format "\n", __FUNCTION__, __LINE__, \
            ##__VA_ARGS__);                                                    \
    } while (0)

#define LOGE(format, ...)                                   \
    do {                                                    \
        printf("\033[0;32;31m"                              \
            "[%s:%d] " format "\033[m"      \
            "\n",                                           \
            __FUNCTION__,  __LINE__, ##__VA_ARGS__);  \
    } while (0)
#endif

int32_t main(int argc, char *argv[])
{
    uint16_t gpio = 380; /* GPIO_01 */
    uint16_t valRead;
    int32_t ret;
    double data = 0;

    LOGD("GpioSetDir GPIO_DIR_OUT\n");

    ret = GpioSetDir(gpio, GPIO_DIR_OUT);
    if (ret != HDF_SUCCESS) {
        LOGE("%s: set dir fail! ret:%d\n", __func__, ret);

        return ret;
    }
    for (;;) {
        data = getadcdata(1);
        

        if(data < 105){
           (void)GpioRead(gpio, &valRead);
           LOGD("GpioRead valRead : %d\n", valRead);
           (void)GpioWrite(gpio, GPIO_VAL_LOW);
           printf("\n************************************************\n");
             printf("\ntemperature = %.2f",data);
             printf("\n************************************************\n");
        }

        else {
             (void)GpioRead(gpio, &valRead);
           LOGD("GpioRead valRead : %d\n", valRead);
           (void)GpioWrite(gpio, GPIO_VAL_HIGH);
           LOGE("%s: wait timeout break!\n", __func__);
             printf("\n************************************************\n");
             printf("\nfinall temperature = %.2f",data);
             printf("\n************************************************\n");
            break;
        }


        OsalMSleep(2000);


    }

    return HDF_SUCCESS;
}
